package com.qf.dao.impl;

import com.qf.dao.DeveloperDao;
import com.qf.pojo.Developer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName DeveloperDaoImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/2 14:19
 * @Version 1.0
 **/
@Repository
public class DeveloperDaoImpl implements DeveloperDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public Developer findDeveloperById(String id) {
        List<Developer> query=null;
        query = jdbcTemplate.query("select * from developer where did=? and status=1",
                new BeanPropertyRowMapper<Developer>(Developer.class), id);
        return query.get(0);
    }

    @Override
    public Developer findDeveloperByName(String name) {
        List<Developer> query=null;
        query = jdbcTemplate.query("select * from developer where dname=? and status=1",
                new BeanPropertyRowMapper<Developer>(Developer.class), name);
        return query.get(0);
    }

    @Override
    public int deletedDeveloper(Developer developer) {

        int update = jdbcTemplate.update("update developer set status=0 where did=? ", developer.getDid());
        int i=0;
        if (update >= 0) {
            i = jdbcTemplate.update("update project set did='' where  did=?", developer.getDid());
        }else {
            return -1;
        }
        return i;
    }

    @Override
    public int updateDeveloperById(Developer developer) {
        Object[] obj={developer.getDname(),developer.getUpdateTime(),developer.getStatus(),developer.getDid()};
        int update = jdbcTemplate.update("update developer set dname=?,updatetime=?,status=? where did=?", obj);
        return update;
    }

    @Override
    public int insertDeveloper(Developer developer) {
        Object[] obj={developer.getDid(),developer.getDname(),developer.getUpdateTime(),developer.getStatus()};
        int update = jdbcTemplate.update("insert into developer values (?,?,?,?)", obj);
        return update;
    }

    @Override
    public List<Developer> findAll() {
        return jdbcTemplate.query("select * from developer;",
                new BeanPropertyRowMapper<Developer>(Developer.class));
    }
}
